package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //套餐分页查询
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //构建套餐条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);//根据传入的参数查找套餐
        //构建分页条件
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //查询分页
        page = setmealMapper.selectPage(page, qw);
        //遍历套餐菜品集合
        List<Setmeal> setmealList = page.getRecords();
        for (Setmeal setmeal : setmealList) {
            //查询套餐分类
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
            //构建菜品条件
            LambdaQueryWrapper<SetmealDish> sdqw = new LambdaQueryWrapper<>();
            sdqw.eq(SetmealDish::getSetmealId, setmeal.getId());//通过菜品套餐id查找套餐
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(sdqw);
            //遍历菜品
            setmeal.setSetmealDishes(setmealDishList);

        }
        return page;


    }


    //套餐新增
    @CacheEvict(value = "setmeal",allEntries = true)//清理整个缓存区域
    @Override
    public void sava(Setmeal setmeal) {
        //新增套餐
        setmealMapper.insert(setmeal);
        log.info("新增之后id：{}", setmeal.getImage());
        //遍历获取的菜品
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());//与套餐id关联
            setmealDishMapper.insert(setmealDish);//传入数据库
        }
    }


    //套餐起售停售
    @CacheEvict(value = "setmeal",allEntries = true)//清理整个缓存区域
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);//通过传入的id查找套餐id
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);//创建实体类传入修改的参数
        setmealMapper.update(setmeal, qw);//将封存的数据传入数据库

    }

    //删除套餐
    @CacheEvict(value = "setmeal",allEntries = true)//清理整个缓存区域
    @Override
    public void deleteBatchIds(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);//通过传入id查找
        qw.eq(Setmeal::getStatus, 1);//判断是否停售
        Integer count = setmealMapper.selectCount(qw);//查询个数
        if (count > 0) {
            throw new CustomException("删除套餐必须停售！");
        }
        //通过传入的id删除套餐
        setmealMapper.deleteBatchIds(ids);

        //删除套餐内菜品关联
        LambdaQueryWrapper<SetmealDish> dqw = new LambdaQueryWrapper<>();
        dqw.in(SetmealDish::getSetmealId, ids);//通过传入id对比关联id
        setmealDishMapper.delete(dqw);
    }

    //套餐修改回显
    @Override
    public Setmeal findById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);//查找传入的id信息
        LambdaQueryWrapper<SetmealDish> dqw = new LambdaQueryWrapper<>();
        dqw.eq(SetmealDish::getSetmealId, id);//通过套餐id查询关联的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(dqw);//查询遍历菜品
        setmeal.setSetmealDishes(setmealDishes);//将菜品封装
        return setmeal;//回显数据
    }

    //套餐修改
    @CacheEvict(value = "setmeal",allEntries = true)//清理整个缓存区域
    @Override
    public void update(Setmeal setmeal) {
        setmealMapper.updateById(setmeal);//修改基本信息
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());//查询传入的菜品id
        setmealDishMapper.delete(qw);//删除查询到的菜品
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();//遍历新添加菜品
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());//将新添加菜品关联套餐id
            setmealDishMapper.insert(setmealDish);//保存数据库
        }
    }

    // 根据套餐id+状态查询套餐列表
    @Cacheable(value = "setmeal",key = "#categoryId")
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //  select * from setmeal where category_id = ? and status = ?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, categoryId);//根据传入id查询套餐
        qw.eq(Setmeal::getStatus, status);//查询状态
        //查询遍历
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        //返回结果
        return setmealList;
    }


    //查询套餐内菜品
    @Override
    public List<Dish> findDishDetail(Long id) {
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);//根据套餐id查找套餐
        //遍历套餐
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);
        ArrayList<Dish> dishList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishList) {
            Dish dish = dishMapper.selectById(setmealDish.getDishId());//查询套餐下的菜品
            dish.setCopies(setmealDish.getCopies());//添加套餐中的数量
            dishList.add(dish);//添加返回集合
        }
        return dishList;//返回结果
    }

}
